#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#define MAXN 1005
#define MAXM 100005
using namespace std;

int components[MAXN];

int main()
{
    int n,m;
    freopen("truth.txt","r", stdin);
    scanf("%d", &n);
    scanf("%d", &m);
    for (int i=0;i<n;i++){
        components[i] = i+1;
    }
    for (int line = 0;line<m;line++){
        int a,b;
        char c;
        scanf("%d", &a);
        scanf("%d", &b);
        a--; b--;
        do
        {
            c = getc(stdin);
        }
        while (c != 'L' && c != 'T');

        if (components[a] == components[b] || components[a] == -components[b]){
            if ((components[a] == components[b]) ^ (c == 'T')){
                printf("%d\n", line);
                return 0;
            }
        } else {
            int acomp = (c=='T'?components[a]:-components[a]);
            int bcomp = components[b];
            for (int i=0;i<n;i++){
                if (components[i] == bcomp){
                    components[i] = acomp;
                } else if (components[i] == -bcomp){
                    components[i] = -acomp;
                }
            }
        }
    }
    printf("%d\n", m);
    return 0;
}
